package com.example.wakaka.locknlock;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class myDB extends SQLiteOpenHelper {
    private static final String DB_NAME= "db_name";
    private static final String password_NAME = "table_name";
    private static final String info_NAME = "info_name";

    private static final int  DB_VERSION = 1;
    SQLiteDatabase sqLiteDatabase;
    private Context context;

    public myDB(Context c) {
        super(c, DB_NAME, null, DB_VERSION);
        context = c;
    }


    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String CREATE_TABLE = "CREATE TABLE if not exists "
                + password_NAME
                + " (_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,password TEXT)";
        sqLiteDatabase.execSQL(CREATE_TABLE);

        String CREATE_TABLE2 = "CREATE TABLE if not exists "
                + info_NAME
                + " (_id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,content TEXT,time TEXT,likenum INTEGER)";
        sqLiteDatabase.execSQL(CREATE_TABLE2);
    }
    public long insert(String name, String password)
    {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("password", password);
        long rid = db.insert(password_NAME, null, values);
        db.close();
        return rid;
    }


    public String search(String name)
    {
        SQLiteDatabase db = getWritableDatabase();
        Cursor cur = db.query(password_NAME,
                null,null, null, null, null, null);
        String password = "";
        for(cur.moveToFirst();!cur.isAfterLast();cur.moveToNext())
        {
            int nameColumn = cur.getColumnIndex("name");
            int phoneColumn = cur.getColumnIndex("password");
            String _name = cur.getString(nameColumn);
            String _passwrd = cur.getString(phoneColumn);
            if(name.equals(_name))
            {
                password = _passwrd;

                break;
            }

        }
        return password;
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
